import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class Ruutvorrand extends JPanel implements ActionListener {
	JLabel at, bt, ct, x1t, x2t;
	JButton vasta, nulli;
	JTextField a, b, c, x1, x2;
	double al, bl, cl, x1l, x2l;
	boolean vastus = true;
	JPanel esimene, teine, kolmas, neljas, viies, kuues, seitsmes;

	public void error(String string) {
		x2.setText("Lahtris \"" + string + "\" peab olema number!");
		x1.setText("");
		vastus = false;
	}

	public Ruutvorrand() {
		
		setLayout(new GridLayout(7,1));
		
		JPanel kkuues = new JPanel(new FlowLayout(FlowLayout.LEADING));

		esimene = new JPanel(new FlowLayout(FlowLayout.LEADING));
		teine = new JPanel(new FlowLayout(FlowLayout.LEADING));
		kolmas = new JPanel(new FlowLayout(FlowLayout.LEADING));
		neljas = new JPanel(new FlowLayout(FlowLayout.LEADING));
		viies = new JPanel(new FlowLayout(FlowLayout.LEADING));
		kuues = new JPanel(new FlowLayout(FlowLayout.LEADING));
		seitsmes = new JPanel(new FlowLayout(FlowLayout.LEADING));




		// LAHTRID
		at = new JLabel("a: ");
		a = new JTextField(10);
		esimene.add(at);
		esimene.add(a);

		bt = new JLabel("b: ");
		b = new JTextField(10);
		teine.add(bt);
		teine.add(b);

		ct = new JLabel("c: ");
		c = new JTextField(10);
		kolmas.add(ct);
		kolmas.add(c);

		x1t = new JLabel("X1: ");
		x1 = new JTextField(25);
		x1.setEditable(false);
		neljas.add(x1t);
		neljas.add(x1);

		x2t = new JLabel("X2: ");
		x2 = new JTextField(25);
		x2.setEditable(false);
		viies.add(x2t);
		viies.add(x2);
		// END OF: LAHTRID


		// VASTA NUPP
		vasta = new JButton("Arvuta");
		vasta.addActionListener(this);
		
		// NULLI NUPP
		nulli = new JButton("Nulli");
		nulli.addActionListener(this);
		
		//Paigutus
		add(esimene);
		add(teine);
		add(kolmas);
		add(neljas);
		add(viies);
		kkuues.add(vasta);
		kkuues.add(nulli);
		add(kkuues);

	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand() == "Arvuta") {
			vastus = true;
			try {
				al = Double.parseDouble(a.getText());
			} catch (NumberFormatException evt) {
				error("a");
			}
			try {
				bl = Long.parseLong(b.getText());
			} catch (NumberFormatException evt) {
				error("b");
			}
			try {
				cl = Double.parseDouble(c.getText());
			} catch (NumberFormatException evt) {
				error("c");
			}
			if (vastus) {
				double be = bl * -1;
				double vastused = Math.pow(bl, 2) - (4 * al * cl);
				if (vastused < 0) {
					x2.setForeground(Color.red);
					x1.setForeground(Color.red);
					x2.setText("Negatiivsest arvust ei saa ruutjuurt võtta!");
					x1.setText("Negatiivsest arvust ei saa ruutjuurt võtta!");
					
				} else {
					vastused = Math.sqrt(vastused);
					double x1e = (be - vastused) / 2 * al;
					double x2e = (be + vastused) / 2 * al;
					x2.setForeground(Color.black);
					x1.setForeground(Color.black);
					x1.setText(x1e + "");
					x2.setText(x2e + "");
				}
			}
		}if (e.getActionCommand() == "Nulli") {
			a.setText("");
			b.setText("");
			c.setText("");
			x1.setText("");
			x2.setText("");
			
		}
	}
}